#!/usr/bin/python

import sys, os
import tempfile
from os import sep, listdir
from distutils.core import setup
from distutils import sysconfig

def getFiles( directory, lst=None ):
    if lst is None: lst = []
    for i in listdir(directory):
        if i == ".svn": continue
        if os.path.isdir(directory+sep+i): lst = getFiles(directory+sep+i, lst)
        else: lst.append( directory+sep+i )
    return lst

if __name__ == '__main__' :

    # install mezoGIS =========================================================
    
    # files with additional data
    datafiles = [ 
        ("share"+sep+"mezogis", ['COPYING','TODO','README']),
        ("share"+sep+"mezogis"+sep+"pixmaps", getFiles("src"+sep+"pixmaps")),
        ("share"+sep+"mezogis"+sep+"palettes", getFiles("src"+sep+"palettes")),
        ("share"+sep+"mezogis"+sep+"plugins", getFiles("src"+sep+"plugins")),
        #("share"+sep+"mezogis"+sep+"locale", getFiles("src"+sep+"locale")),
                                
        #("share"+sep+"mezogis"+sep+"doc", getFiles("doc")),
        ("share"+sep+"pixmaps", ["src"+sep+"mezogis.svg"]),
        ("share"+sep+"applications",['src'+sep+'mezogis.desktop']),
    ]

    # add locale files to datafiles
    for f in getFiles("locale"):
        datafiles.append( (os.path.split("share"+sep+f)[0], [f]) )
    
    # launch setup
    setup(\
        name         = "mezoGIS",
        version      = "0.1.5",
        license      = "GPL",
        description  = "Perform PostGis Operations",
        author       = "Frederic Back",
        author_email = "fredericback@gmail.com",
        url          = "http://www.mezogis.org/",
        packages     = ['mezoGISlib'],
        package_dir  = {'mezoGISlib': 'src'+sep+'mezogislib'},
        data_files   = datafiles,
        scripts      = ['src/mezogis.py']
    )

    # install geoTypes =========================================================
    print
    reinstall = True
    minVersion = (0,4,1)

    # check if GeoTypes is already installed
    try:
        import GeoTypes
        versionFile = sysconfig.get_python_lib()+sep+'GeoTypes'+sep+'VERSION'
        f = open(versionFile,"r") # get version number
        r = f.read().strip()
        print "GeoTypes "+r+" has been found on your system."
        v = r.split(".")
        
        if (int(v[0]),int(v[1]),int(v[2])) >= minVersion:
            reinstall = False
    except: print "GeoTypes has not been found on your system."

    # check if GeoTypes is included in the package (not the case if pulled from svn)
    if reinstall and os.access("GeoTypes",os.F_OK):
        print "mezoGIS requires GeoTypes %s.%s.%s and higher to run." % minVersion
        sys.stdout.write( "Do you want to install it now (Y/n)? " )
        c = sys.stdin.read(1)
        print
        print
        if c != "n":

            ################################################################################
            # Copyright (c) QinetiQ Plc 2003
            #
            # Licensed under the LGPL. For full license details see the LICENSE file.
            ################################################################################


            DOC_FILES = ('GeoTypes'+sep+'LICENSE','GeoTypes'+sep+'NEWS',
                'GeoTypes'+sep+'INSTALL', 'GeoTypes'+sep+'README', 'GeoTypes'+sep+'VERSION',
                'GeoTypes'+sep+'KNOWN_BUGS', 'GeoTypes'+sep+'AUTHORS', 'GeoTypes'+sep+'TODO')
            
            LONG_DESCRIPTION = "GeoTypes"+\
                "A package of classes for working with basic 2d geometric types."+\
                "These classes were designed for use with the geometric functions"+\
                "supported by Postgresql (http://www.postgresql.com) although they"+\
                "do not need Postgresql to be present for them to be used."

            install_dir = sysconfig.get_python_lib() + sep + 'GeoTypes'

            setup(name                  = "GeoTypes",
                    version               = "0.4.1",
                    license               = "LGPL",
                    description           = "Classes for working with basic 2d geometric types",
                    author                = "Richard Taylor, QinetiQ Plc.",
                    author_email          = "r.taylor@eris.qinetiq.com",
                    url                   = "http://",
                    packages              = ['GeoTypes'],
                    package_dir             = {'GeoTypes': 'GeoTypes'+sep+"GeoTypes"},
                    data_files            = [ (install_dir, DOC_FILES) ],
                    long_description      = LONG_DESCRIPTION
            )

    # check for psycopg =========================================================
    try:
        import psycopg
    except:
        print
        print "mezoGIS requires the psycopg module to run,"
        print "but it could not be found on your system."
        print
        print "download it from: http://initd.org/projects/psycopg1"
